package com.kejiguan.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.kejiguan.ui.R;
import com.kejiguan.ui.model.MenuItem;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by YYY on 16/8/23.
 */
public class ManagerUserMenuAdapter extends BaseAdapter implements View.OnClickListener{

    private Context context;
    private List<MenuItem> menuItems;
    private LayoutInflater inflater;
    private List<Boolean> selectStates;



    public ManagerUserMenuAdapter(Context context, List<MenuItem> menuItems) {
        this.context = context;
        this.menuItems = menuItems;
        initStates();
        inflater = LayoutInflater.from(context);
    }



    @Override
    public int getCount() {
        if (null == menuItems) {
            return 0;
        } else {
            return menuItems.size();
        }
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (null==convertView)
        {
            convertView=inflater.inflate(R.layout.dialog_add_user_menu_item_layout,null);
        }

        MenuItem menuItem = menuItems.get(position);

        if (selectStates.get(position))
        {
            ((ImageView)convertView.findViewById(R.id.select_state)).setImageResource(R.mipmap.kjg_menu_checked);
        }else {
            ((ImageView)convertView.findViewById(R.id.select_state)).setImageResource(R.mipmap.kjg_menu_uncheck);
        }

        ((TextView)convertView.findViewById(R.id.menu_name)).setText(menuItem.getLabel());
        convertView.setTag(R.id.recyclerview_item_position, position);
        convertView.setOnClickListener(this);

        return convertView;
    }

    private void initStates() {
        if (getCount() > 0) {
            selectStates = new ArrayList<>();
            for (int i = 0; i < getCount(); i++) {
                selectStates.add(false);
            }
        }
    }


    public void setHasSelctedMenus(String navList)
    {

        if (null!=navList)
        {
            String[] menus=navList.split(",");
            if (null!=menus&&menuItems!=null)
            {
                for (String menuid:menus)
                {
                    for (int i=0;i<getCount();i++)
                    {
                        if (menuid.equals(String.valueOf(menuItems.get(i).getMenuId())))
                        {
                            selectStates.set(i,true);
                        }
                    }
                }
            }
        }

    }

    public String getSelectMenus()
    {
        StringBuffer menus=new StringBuffer();
        for (int i=0;i<getCount();i++)
        {
            if (selectStates.get(i))
            {
                menus.append(menuItems.get(i).getMenuId()).append(",");
            }

        }
        return menus.toString();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add_menu_item:
                int position = (int) v.getTag(R.id.recyclerview_item_position);
                selectStates.set(position, selectStates.get(position) ? false : true);
                notifyDataSetChanged();
                break;
        }
    }
}
